#include "ftp.h"
#define LENNAM 21
void pressKey(){
    printf("press any key to continue……\n");
    fflush(stdin);
    sleep(5);
    fgetc(stdin);
}
//返回0登录成功
int menu(socket_t sfd){
    char sel;
    while(1){
        sel = 0;
        system("clear");
        printf("\t1- login\n\t2- signup\n");
        printf("\t3- test1\n\t4- test2\n");

        printf("press NumKey to select:\n");
        rewind(stdin),scanf("%c",&sel);
        if('1' == sel){
            if(0 == login(sfd)){
                return 0;
            }
        }
        else if ('2' == sel){
            if(0 != signup(sfd)){
                printf("sigup failed!\n");
            }
            else {
                printf("signup success!\n");
            }
            pressKey();
            continue;
        }
        else if ('3' == sel || '4' == sel){
            printf("wait for more\n");
            pressKey();
            continue;
        }
        else {
            printf("input wrong!\n");
            continue;
        }
    }
}
int signup(socket_t sfd){
    char name[LENNAM];
    char pwd[LENNAM];
    char pwdc[LENNAM];
    printf("input new username:\n");
    scanf("%s",name);

    printf("input your password:\n");
    scanf("%s",pwd);
    printf("repeat password for check:\n");
    scanf("%s",pwdc);
    if(0 != strcmp(pwd, pwdc)){
        printf("CHECK WRONG!\n");
        return -1;
    }
    printf("waiting\n");
    int flag = 2;//注册信息写成功后，再发送注册标志
    send(sfd, &flag, 4, 0);

    printf("wait after\n");
    int len = strlen(name);//先发账号
    send(sfd, &len, 4, 0);
    send(sfd, name, len, 0);

    char ack[5] = "";
    len = 5;
    recv(sfd, &len, 4, 0);
    recvn(sfd, ack, len);
    printf("继续确认%s\n",ack);
    if(strncmp(ack, "0000", 4) == 0){
        printf("username reapeted\n");
        return -1;
    }
    

    len = strlen(pwd);//再发密码
    printf("send pwd,len = %d\n",len);
    send(sfd, &len, 4, 0);
    send(sfd, pwd, len, 0);
    return 0;
}
void sendAck(socket_t sfd, char *ack, int len) {
    send(sfd, &len, 4, 0);
    sendn(sfd, ack, len);
}
int login(socket_t sfd) {

    int flag = 1;//先发送登录标志，再进行登录确认
    send(sfd, &flag, 4, 0);

    char name[LENNAM] = "";
    char pwd[LENNAM] = "";
    //判断非法输入
    while(1){
        system("clear");
        printf("input username:");
        bzero(name, LENNAM);
        scanf("%s",name);
        if(0 != name[LENNAM-1]){
            printf("input err : too long\n");
            pressKey();
        }
        else {
            break;
        }
    }
    //发送username
    int len = strlen(name);
    send(sfd, &len, 4, 0);
    sendn(sfd, name, len);
    //接收salt
    char salt[20] = "";
    recv(sfd, &len, 4, 0);
    recvn(sfd, salt, len);
<<<<<<< HEAD

    //salt临时作为验证标志，
    //当用户名不存在时，接收到fail验证，存在则是正确salt值
    //len == 4 && 
=======
    puts(salt);
>>>>>>> c0dd19eca2f7ae81c7b637b7bd48ebd771eb2007
    if(strncmp(salt, "fail", 4) == 0){
        printf("username = [%s]\n",name);
        printf("username wrong!\n");
        exit(-1);
    }

    //输入和检测password合法性
    while(1){
        system("clear");
        printf("input password:");
        bzero(pwd, LENNAM);
        scanf("%s",pwd);
        if(0 != name[LENNAM-1]){
            printf("input err : too long\n");
            pressKey();
        }
        else {
            break;
        }
    }
<<<<<<< HEAD
    //发送密文密码
    char cryptPwd[100] ;
=======
    char  cryptPwd[100];
>>>>>>> c0dd19eca2f7ae81c7b637b7bd48ebd771eb2007
    strcpy(cryptPwd,crypt(pwd,salt));
    
    
    
    
    
    len = strlen(cryptPwd);
    send(sfd, &len, 4, 0);
    sendn(sfd, cryptPwd, len);
<<<<<<< HEAD

    //接受密码验证
=======
    
>>>>>>> c0dd19eca2f7ae81c7b637b7bd48ebd771eb2007
    char ack[5];
    recv(sfd, &len, 4, 0);
    recvn(sfd, ack, len);
    if(strncmp(ack, "1010", 4) == 0){
        printf("验证通过!\n");
        return 0;
    }
    else {
        printf("验证失败!\n");
        exit(-1);
    }
}
